import Vue from 'vue'
import VueRouter from 'vue-router'

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject){
    return originalPush.call(this, location, onResolve, onReject)
  }
  return originalPush.call(this, location).catch(err => err)
}


import NProgress from "nprogress"; // 进度条；进度指示器
import "nprogress/nprogress.css"; // 进度条；进度指示器 样式// 配置
NProgress.inc(0.2);
NProgress.configure({
	easing: "ease",
	speed: 500,
	showSpinner: false
});

import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/404.vue')
  },
  {
    path: '*', redirect: '/404'
  }
]


const scrollBehavior = (to, from, savedPosition) => {
  // console.log('滚动行为', to, from, savedPosition)
  return { x: 0, y: 0 };
};


const router = new VueRouter({
  routes,
  scrollBehavior
})

router.beforeEach((to, from, next) => {
  let token = localStorage.getItem("token");
  NProgress.start();
  next();

  // if (!token && to.meta.requireAuth) {
  //   // debugger
  //   alertErr("请您先登录");
  //   next("/login");
  // } else {
  //   next();
  // }
});

router.afterEach((to, from) => {
  NProgress.done();
});

export default router
